package com.isczy.eduorder.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.api.R;
import com.isczy.eduorder.entity.Order;
import com.isczy.eduorder.service.OrderService;
import com.isczy.servicebase.exceptionHandler.MyException;
import com.isczy.utils.DataResult;
import com.isczy.utils.JwtUtils;
import com.isczy.utils.ResultCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 * 订单 前端控制器
 * </p>
 *
 */
@RestController
@RequestMapping("/eduorder/order")
@CrossOrigin
public class OrderController {

    @Autowired
    private OrderService orderService;

    /***
     * ***************************************
     * 功能描述:生成订单的方法
     * @param courseId
     * @param request
     * @return {@link R}
     * @author create: TODO 人员:【ChangZiYang】类型:【新增方法】日期:【2021-01-21 15:50】
     * @author modify:
     */
    @PostMapping("createOrder/{courseId}")
    public DataResult saveOrder(@PathVariable String courseId, HttpServletRequest request) {
        try{
            //创建订单，返回订单号
            String orderNo =orderService.createOrders(courseId, JwtUtils.getMemberIdByJwtToken(request));
            return DataResult.builder().data("orderId",orderNo).successBuild();
        }catch (Exception e){
            throw MyException.builder()
                    .exception(e)
                    .code(ResultCode.ERROR)
                    .className(getClass().getName())
                    .methodName(Thread.currentThread().getStackTrace()[1].getMethodName())
                    .message("生成订单失败")
                    .params(courseId)
                    .build();
        }

    }

    /***
     * ***************************************
     * 功能描述:根据订单id查询订单信息
     * @param orderId
     * @return {@link R}
     * @author create: TODO 人员:【ChangZiYang】类型:【新增方法】日期:【2021-01-21 15:52】
     * @author modify:
     */
    @GetMapping("getOrderInfo/{orderId}")
    public DataResult getOrderInfo(@PathVariable String orderId) {
        try{
            QueryWrapper<Order> wrapper = new QueryWrapper<>();
            wrapper.eq("order_no",orderId);
            Order order = orderService.getOne(wrapper);
            return DataResult.builder().data("item",order).successBuild();
        }catch (Exception e){
            throw MyException.builder()
                    .exception(e)
                    .code(ResultCode.ERROR)
                    .className(getClass().getName())
                    .methodName(Thread.currentThread().getStackTrace()[1].getMethodName())
                    .message("根据订单id查询订单信息失败")
                    .params(orderId)
                    .build();
        }
    }
    /***
     * ***************************************
     * 功能描述:根据课程id和用户id查询订单表中订单状态
     * @param courseId
     * @param memberId
     * @return {@link boolean}
     * @author create: TODO 人员:【ChangZiYang】类型:【新增方法】日期:【2021-01-22 11:45】
     * @author modify:
     */
    @GetMapping("isBuyCourse/{courseId}/{memberId}")
    public boolean isBuyCourse(@PathVariable String courseId,@PathVariable String memberId) {
        QueryWrapper<Order> wrapper = new QueryWrapper<>();
        wrapper.eq("course_id",courseId);
        wrapper.eq("member_id",memberId);
        wrapper.eq("status",1);//支付状态 1代表已经支付
        int count = orderService.count(wrapper);
        if(count>0) { //已经支付
            return true;
        } else {
            return false;
        }
    }

}

